package com.cuz.hot100;


import com.cuz.hot100.datastruck.ListNode;

public class Q83deleteDuplicates {
    public static void main(String[] args) {
        ListNode listNode = ListNode.creatBy(1, 1, 1, 2, 3, 3);
        deleteDuplicates1(listNode).print();
    }

    public static ListNode deleteDuplicates1(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode preHead = new ListNode();
        preHead.next = head;
        ListNode cur = head;
        ListNode pre = preHead;
        ListNode next = cur.next;
        while (next != null) {
            while (next != null && cur.val == next.val) {
                next = next.next;
            }
            pre.next = cur;
            pre = pre.next;
            cur = next;
            if (next==null){
                break;
            }
            next = next.next;
        }
        if (pre.next != cur) {
            pre.next = cur;
        }
        return preHead.next;
    }
}
